package net.xxcl.stuManager.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.xxcl.stuManager.mapper.TeacherMapper;
import net.xxcl.stuManager.pojo.Teacher;
import net.xxcl.stuManager.util.DigestUtil;

public class LoginCheckAction extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String name = request.getParameter("name");
		String password = request.getParameter("password");
		password = DigestUtil.encodeSHA512Hex(password.getBytes());
		if(request.getParameter("role").equals("1")){			
			TeacherMapper teacherMapper = new TeacherMapper();
			List<Teacher> list = teacherMapper.login(name, password);
			if(list.size()>0){
				Teacher teacher = list.get(0);
				request.getSession().setAttribute("teacher", teacher);
				if(teacher.getRole()==0){
					//跳转到超级管理员界面
					response.sendRedirect("");
				} else if(teacher.getRole()==1){
					//跳转到系部管理员界面
					response.sendRedirect("");
				} else if(teacher.getRole()==2){
					//跳转到辅导员界面
					response.sendRedirect("mentor/index.jsp");
				} else {
					
				}
			} else {
				request.setAttribute("message", "用户名或者密码错误！");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		} else {
			//学生登录
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
